package com.Mobile.Action;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;

import transformFactory.DateJsonValueProcessor;

import com.Entity.Userdata;
import com.Entity.Users;
import com.Mobile.Entity.MobileUsers;
import com.Service.UserdataService;
import com.Service.UsersService;

public class MobileLoginAction implements ServletResponseAware  {

	private HttpServletResponse response;//包含登录和注册两部分
	private int flag=0;
	@Autowired
	private UsersService usersservice;
	@Autowired
	private UserdataService userdataService;
	private Users user=new Users();
	private Userdata userdata=new Userdata();
	private MobileUsers mobileusers;	
	public UserdataService getUserdataService() {
		return userdataService;
	}
	public void setUserdataService(UserdataService userdataService) {
		this.userdataService = userdataService;
	}
	public UsersService getUsersservice() {
		return usersservice;
	}
	public void setUsersservice(UsersService usersservice) {
		this.usersservice = usersservice;
	}
	
	public Users getUser() {
		return user;
	}
	public void setUser(Users user) {
		this.user = user;
	}
	//登录
	public void check() throws ServletException,IOException {
	
		String userName=ServletActionContext.getRequest().getParameter("username");
		String password=ServletActionContext.getRequest().getParameter("password");
		
		userName = new String(userName.getBytes("ISO-8859-1"), "UTF-8");
	
		System.out.println(userName);
		userName = java.net.URLDecoder.decode(userName,"UTF-8");
		password = java.net.URLDecoder.decode(password,"UTF-8");
		
		JSONObject returnjson=new JSONObject();
		JSONArray jsonarray=new JSONArray();
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset=utf-8"); 
		try {
			PrintWriter out = response.getWriter();
			List<Users> userlist = this.usersservice.CheckLogin(userName,password);
			System.out.println(userlist.isEmpty());
			if(userlist!=null&&!userlist.isEmpty()){
				flag=1;									
				user=userlist.get(0);				
				int id=user.getUserId();								
				Userdata userdata=this.userdataService.getById(id);																
				 ArrayList list = new ArrayList();				 
				 
				 MobileUsers mobileusers=new MobileUsers();
				 mobileusers.setCardId(user.getUserId());
				 mobileusers.setCardPass(userdata.getCardPass());
				 mobileusers.setCity(userdata.getCity());
				 mobileusers.setCommunityId(user.getCommunityId());
				 mobileusers.setCounty(userdata.getCounty());
				 mobileusers.setDays(userdata.getDays());
				 mobileusers.setDeviceId(user.getDeviceId());
				 mobileusers.setExpectedDateBirth(userdata.getExpectedDateBirth());
				 mobileusers.setIdcard(userdata.getIdcard());
				 //mobileusers.setLastActivityTime(user.getLastActivityTime());
				 mobileusers.setLoginTimes(user.getLoginTimes());
				 mobileusers.setLoginUserShowName(user.getLoginUserShowName());
				 mobileusers.setMonths(userdata.getMonths());
				 mobileusers.setNickName(user.getNickName());
				 mobileusers.setPassword(user.getPassword());
				 mobileusers.setPasswordMd5(user.getPasswordMd5());
				 mobileusers.setPregnancyDate(userdata.getPregnancyDate());
				 mobileusers.setPrivateEmail(user.getPrivateEmail());
				 mobileusers.setProvince(userdata.getProvince());
				 mobileusers.setRank(user.getRank());
				 mobileusers.setRealName(userdata.getRealName());
				 mobileusers.setSex(userdata.getSex());
				 mobileusers.setSignature(userdata.getSignature());				 
				 mobileusers.setUserAccountStatus(user.getUserAccountStatus());
				 mobileusers.setUserAddress(userdata.getUserAddress());
				 mobileusers.setUserId(userdata.getUserId());
				 mobileusers.setUserName(user.getUserName());
				 mobileusers.setUserType(user.getUserType());
				 mobileusers.setYears(userdata.getYears());
				 mobileusers.setPhoneNumber(userdata.getPhoneNumber());
				 System.out.println(userdata.getPhoneNumber());
				 
				 mobileusers.setDate(new Date());				 
				 list.add(mobileusers);				 
				 JsonConfig jsonConfig = new JsonConfig();
				  jsonConfig.registerJsonValueProcessor(Date.class,
				  new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
				  JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);

				  System.out.println(jsonArray.toString());	
				
				/*Date ExpectedDateBirth1=userdata.getExpectedDateBirth();
				Date PregnancyDate1=userdata.getPregnancyDate();
				
				String ExpectedDateBirth = String.valueOf(ExpectedDateBirth1);//閻劋绨崚銈嗘焽ExpectedDateBirth1閺勵垰鎯佹稉铏光敄閿涘奔璐熺粚楦跨箲閸ョ偛鐡х粭锔胯null閵?
				String PregnancyDate = String.valueOf(PregnancyDate1);
								*/				
				//JSONArray jsonbean=JSONArray.fromObject(list);
				returnjson.put("flag",flag);
				returnjson.put("info",jsonArray);
				returnjson.put("info",jsonArray);
				
			}
			else{
				flag=0;//闂佽皫鍡╁殭缂傚秴绉靛鍕綇椤愩儛?
				returnjson.put("flag", flag);
			}						
			out.println(returnjson.toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	//注册register
	public void register() throws ServletException,IOException {
		
		String username=ServletActionContext.getRequest().getParameter("UserName");
		String password=ServletActionContext.getRequest().getParameter("Password");
		String privateemail=ServletActionContext.getRequest().getParameter("PrivateEmail");
		String sex=ServletActionContext.getRequest().getParameter("Sex");
		
		username = new String(username.getBytes("ISO-8859-1"), "UTF-8");
		System.out.println(username);
		sex = new String(sex.getBytes("ISO-8859-1"), "UTF-8");
		
		username = java.net.URLDecoder.decode(username,"UTF-8");
		sex = java.net.URLDecoder.decode(sex,"UTF-8");
		privateemail = java.net.URLDecoder.decode(privateemail,"UTF-8");
		password = java.net.URLDecoder.decode(password,"UTF-8");
		
		JSONObject returnjson=new JSONObject();
		
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset=utf-8"); 
		try {
			PrintWriter out = response.getWriter();
			
			List<Users> userlist= this.usersservice.findByPrivateEmail(privateemail);
			System.out.println(userlist.isEmpty());
			List<Users> userlist1=this.usersservice.CheckLogin(username, password);
			if(userlist!=null&&userlist.size()!=0){//邮箱不可以重复注册
				flag=0;	
				returnjson.put("flag", flag);
				returnjson.put("info", "邮箱重复");
			}
			else if(userlist1!=null&&userlist1.size()!=0){
				flag=1;
				returnjson.put("flag", flag);
				returnjson.put("info", "用户名和密码重复，请改掉其中之一");
			}
			
			else {
				 flag=2;			
				 Users user=new Users();
				 Userdata userdata=new Userdata();				
				 
				 user.setUserName(username);
				 user.setPassword(password);				 
				 user.setPrivateEmail(privateemail);				 								 			 
				 user.setUserAccountStatus((short) 0);				 				 
				 user.setUserType((short) 1);
				 this.usersservice.save(user);
				 
				 List<Users> userlist2=this.usersservice.CheckLogin(username, password);
				 user=userlist2.get(0);
				 int id=user.getUserId();
				 userdata.setSex(sex);
				 userdata.setUserId(id);
				 this.userdataService.save(userdata);				 
				 returnjson.put("flag",flag);
				 returnjson.put("info","注册成功");
				 
			}						
			out.println(returnjson.toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
	@Override
	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response=response;
	}
	public MobileUsers getMobileusers() {
		return mobileusers;
	}
	public void setMobileusers(MobileUsers mobileusers) {
		this.mobileusers = mobileusers;
	}
	public Userdata getUserdata() {
		return userdata;
	}
	public void setUserdata(Userdata userdata) {
		this.userdata = userdata;
	}
	
	
	
	
	
}
